package com.shqcjd.sinopecdemomobile.ui.activity.tipwizard;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shqcjd.sinopecdemomobile.R;
import com.shqcjd.sinopecdemomobile.api.ApiService;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxObserver;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.sinopecdemomobile.model.ResponseData;
import com.shqcjd.sinopecdemomobile.ui.activity.preview.LandScapeVideosActivity;
import com.shqcjd.sinopecdemomobile.ui.adapter.TipWizardVideoAdapter;
import com.shqcjd.sinopecdemomobile.ui.base.BaseActivity;
import com.shqcjd.sinopecdemomobile.ui.base.BasePresenter;
import com.shqcjd.sinopecdemomobile.util.SpacesItemDecoration;
import com.shqcjd.sinopecdemomobile.util.UIUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * 操作指导视频列表
 */
public class TipWizardVideoActivity extends BaseActivity {


    @BindView(R.id.rv_video_list)
    RecyclerView rvVideoList;
    @BindView(R.id.ib_finish)
    ImageButton ibFinish;

    private int stepDetailId;

    private View notDataView;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_tip_wizard_video;
    }

    @Override
    public void initView() {
        stepDetailId = getIntent().getIntExtra("stepDetailId",0);
        if (stepDetailId==0){
            finish();
            return;
        }
        rvVideoList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));

        //添加ItemDecoration，item之间的间隔
        int leftRight = UIUtils.dip2px(18);
        int topBottom = UIUtils.dip2px(0);
        rvVideoList.addItemDecoration(new SpacesItemDecoration(leftRight, topBottom));
        notDataView = getLayoutInflater().inflate(R.layout.layout_no_data, (ViewGroup) rvVideoList.getParent(), false);
        fetchData();
    }

    private void fetchData(){
        showWaitingDialog("正在获取数据");
        ApiService.getMediaList(stepDetailId)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<ArrayList<String>>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                    }

                    @Override
                    public void _onNext(ResponseData<ArrayList<String>> retData) {
                        if (retData.retCode == 10000 ) {
                            TipWizardVideoAdapter tipWizardVideoAdapter = new TipWizardVideoAdapter();
                            if (retData.data == null || retData.data.isEmpty()){
                                tipWizardVideoAdapter.setEmptyView(notDataView);
                            }else {
                                tipWizardVideoAdapter.setNewData(retData.data);
                                tipWizardVideoAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {

                                    @Override
                                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                        Intent intent = new Intent(TipWizardVideoActivity.this, LandScapeVideosActivity.class);
                                        intent.putExtra("url",retData.data.get(position));
//                                        intent.putExtra("name",retData.data.get(position));
                                        startActivity(intent);
                                    }
                                });
                            }
                            rvVideoList.setAdapter(tipWizardVideoAdapter);
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });
    }

    @OnClick(R.id.ib_finish)
    public void onViewClicked() {
        finish();
    }
}
